#include "StdAfx.h"
#include <utility>
#include "SquareLine.h"
#include "LocalPane.h"

CSquareLine::CSquareLine(CLocalPane* pSquarePane, CPoint point, UINT nSquareCount, UINT nSquareWidth)
	: m_pSquarePane(pSquarePane)
	, m_pntLeftTop(point)
	, m_nSquareCount(nSquareCount)
	, m_nSquareWidth(nSquareWidth)
{
	ASSERT(m_pSquarePane != NULL);

	// Construct square line
	for (int i = 0; i < m_nSquareCount; i++)
	{
		CPoint pnt;
		pnt.x = m_pntLeftTop.x + i * m_nSquareWidth;
		pnt.y = m_pntLeftTop.y;

		m_vecSquare.push_back(CSquare(m_pSquarePane, pnt, m_nSquareWidth, false, RGB(255,255,255)));
	}

	m_pntRightBottom.x = m_pntLeftTop.x + m_nSquareCount * m_nSquareWidth;
	m_pntRightBottom.y = m_pntLeftTop.y + m_nSquareWidth;
}

CSquareLine::~CSquareLine(void)
{
}

void CSquareLine::Draw(CDC* pDC)
{
	for (SquareVecIter iter = m_vecSquare.begin(); iter != m_vecSquare.end(); ++iter)
	{
		iter->Draw(pDC);
	}
}

void CSquareLine::Fill(CDC* pDC)
{
    for (SquareVecIter iter = m_vecSquare.begin(); iter != m_vecSquare.end(); ++iter)
	{
		iter->Fill(pDC);
	}
}

bool CSquareLine::SquareInLine(const CSquare& square)
{
	bool bResult = false;

	for (SquareVecIter iter = m_vecSquare.begin(); iter != m_vecSquare.end(); ++iter)
	{
		if( *iter == square )
		{
			bResult = true;
			break;
		}
	}

	return bResult;
}

void CSquareLine::SwapLineState(CSquareLine& squareLine)
{
	for (int i = 0; i < m_nSquareCount; ++i)
	{
        CSquare& local_square = m_vecSquare[i];
        CSquare& outer_square = squareLine[i];

		bool bTemp = local_square.GetFlag();
		local_square.SetFlag(outer_square.GetFlag());
		outer_square.SetFlag(bTemp);

		COLORREF color = local_square.GetColor();
		local_square.SetColor(outer_square.GetColor());
		outer_square.SetColor(color);
	}
}

bool CSquareLine::IsEmpty() const
{
	bool bResult = true;
	for (int i = 0; i < m_nSquareCount; i++)
	{
		if (m_vecSquare[i].GetFlag())
		{
			bResult = false;
			break;
		}
	}

	return bResult;
}

bool CSquareLine::IsFull() const
{
	bool bResult = true;
	for (int i = 0; i < m_nSquareCount; i++)
	{
		if (!m_vecSquare[i].GetFlag())
		{
			bResult = false;
			break;
		}
	}

	return bResult;
}

void CSquareLine::SetEmpty()
{
	for (int i = 0; i < m_nSquareCount; i++)
	{
		m_vecSquare[i].SetFlag(false);
	}
}

UINT CSquareLine::GetSquareCount() const
{
    return m_nSquareCount;
}

UINT CSquareLine::GetSquareWidth() const
{
    return m_nSquareWidth;
}

CSquare& CSquareLine::operator[](UINT nIndex)
{
    ASSERT(nIndex >=0 && nIndex < m_nSquareCount);

    return m_vecSquare[nIndex];
}